//
//  XMLoginTool.m
//  XiongMaoJiaSu
//
//  Created by 徐汉卿 on 16/5/26.
//  Copyright © 2016年 ISOYasser. All rights reserved.
//

#import "XMLoginTool.h"







NSString *const kYingCangGouMai = @"kYingCangGouMai";



@interface XMLoginTool()
/**
 *  通过广告标示 登陆  不显示登陆界面
 */
+(void)dengLu;



/**
 *  存储用户模型到本地
 */
+(void)saveModel:(id)model;





/**
 *  如果用户点击了退出 下次用户打开APP就不要再 去访问网络请求登录
 *
 *  @param needLogin <#needLogin description#>
 */
//+(void)setNeedLogin:(BOOL)needLogin;

/**
 *  检测用户最后的操作是 登录了 还是退出了
 *
 *  @return <#return value description#>
 */
+(BOOL)needLogin;

@end



@implementation XMLoginTool
//single_implementation(XMLoginTool)

//+(void)dengLuWithSucessBlock:(void (^)())block fromVC:(UIViewController *)vc
//{
//    
//        //没登陆返回NO
//        XMLoginVC *lvc =[[XMLoginVC alloc] init];
//        if (block) {
//            lvc.loginSuccessBlock = block;
//        }
//        
//        PushNAVViewController *nav = [[PushNAVViewController alloc] initWithRootViewController:lvc];
//        if (vc) {
//            [vc presentViewController:nav animated:YES completion:nil];
//        }else{
//            UIViewController *tempVc = [UIViewController currentViewController];
//            NSLog(@"tempVc = %@",tempVc);
//            [tempVc presentViewController:nav animated:YES completion:nil];
//            
//        }
//        
// 
//    
//}
//
//
//
//+(void)tuiChu
//{
//    
//    //不需要发送网络请求
//    //把本地的模型删掉
//    
//    
//    
//    NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
//    dicM[@"device_type"] = @"IPHONE";
//    dicM[@"hardware_sn"] = GGBS;
//  
//    
//    WS(weakSelf);
//    
//    //发送网络请求
//    [XHQHttpTool xhqpost:[kNetAdressBase stringByAppendingString:kNetAdressLogout] params:dicM success:^(NSString *urlStr, NSDictionary *json) {
//        if ([json[@"success"] intValue] == YES) {//[json[@"success"] intValue] == YES
//            
//            [XHQArchiverTool writeModel:nil to:kFilePathUserModel];
////            [weakSelf setNeedLogin:NO];
//            [NSDC postNotificationName:kNotifLoginStaeChage object:nil];
//            
//        }else{
//            
//            NSString *msg =   json[@"msg"];
//            [msg showAlert];
//        }
//        
//        
//    } failure:^(NSString *urlStr, NSError *error) {
//        [error.localizedDescription showAlert];
//    } timeOut:0 showMB:YES toView:nil];
//    
//
//    
//   
//
//    
//    
//   
//
//}
//
//
//
//+(void)load
//{
//    
//    [self xhqReturn_guanwang_buy];
//    //要不要登录呢?   在沙盒里保存用户上次有没有点击退出   如果最后的操作的点击退出  则下次打开app不自动连接    如果最后的状态是连接  则自动登录
//    //本地沙盒(nsuesrde)要存个要不要登录的文件
////    if ([self needLogin]) {
////        [self dengLu];
////    }
//    //默认第一次安装APP 是要登录的 如果用户点击退出 则APP 没有死的话 他不会再等 如果死了再打开 会不会登录呢?
//    //如果app被挤掉 提示账号异地登录
//    
//
//  
////    [self checkMessageCount];
////       [NSTimer scheduledTimerWithTimeInterval:10  target:self selector:@selector(checkMessageCount) userInfo:nil repeats:YES];
//    
//
//
//    
//        //当用户登录了  去访问 反馈model
//        //自己接受登录通知  然后登录成功去访问 checkFankuiModel
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginStaeChanged) name:kNotifLoginStaeChage object:nil];
//    
//    
//    
//    //自己观察
////    [self checkMessageCount];
////    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(checkMessageCount) name:kNotifLoginStaeChage object:nil];
//    
//    
//    //检测账号密码修改
//    [self checkUserAccountChange];
//    [NSTimer scheduledTimerWithTimeInterval:10 * 60  target:self selector:@selector(checkUserAccountChange) userInfo:nil repeats:YES];
//    
//    /**
//     *消息检测方法
//     */
//    [self jianCeXiaoXi];
//    
//     [NSTimer scheduledTimerWithTimeInterval:10 * 60   target:self selector:@selector(jianCeXiaoXi) userInfo:nil repeats:YES];
//
//}
//
//+(void)loginStaeChanged
//{
//    //让消息个数模型都清空再去获取
////    [XHQArchiverTool writeModel:nil to:kFilePathNoReadXitongModel];
////    [XHQArchiverTool writeModel:nil to:kFilePathNoReadFankuiModel];
//
//
//    [self jianCeXiaoXi];
//}
//
//
//+(void)jianCeXiaoXi
//{
//    
//    NOReadModel *noReadModel = [XHQArchiverTool readModelWithPath:kFilePathNoReadModel];
//     NSLog(@"xhq%@",noReadModel);
//    if (noReadModel == nil) {
//        [NoReadModelTool saveNilModel];
//        noReadModel = [XHQArchiverTool readModelWithPath:kFilePathNoReadModel];
//    }
//    
//
//    //    检测未读消息个数  方法
//    if ([self readModel] == nil) {
//        [self checkXiTongModel];//请求系统消息
//        
//    }else{
//        [self checkXiTongModel];//请求系统消息
//        [self checkFankuiModel];
//    }
//}
//
//
//+(void)checkXiTongModel
//{
//    NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
//    dicM[@"device_type"] = @"IPHONE";
//    dicM[@"hardware_sn"] = GGBS;
//    
//    
//    [XHQHttpTool xhqpost:[kNetAdressBase stringByAppendingString:@"get_message_count"] params:dicM success:^(NSString *urlStr, NSDictionary *json) {
//        if ([json[@"success"] intValue] == YES) {//[json[@"success"] intValue] == YES
//            
//            
//            XMNoReadXitongModel *model = [XMNoReadXitongModel yy_modelWithJSON:json];
//            
//            //取出本地的NoReadModel
//            //
//            NOReadModel *noReadModel = [XHQArchiverTool readModelWithPath:kFilePathNoReadModel];
//            if (noReadModel == nil) return ;
//            noReadModel.noReadXitongModelXin = model;
//            noReadModel.noReadXitongModelShow.system_message = noReadModel.noReadXitongModelXin.system_message - noReadModel.noReadXitongModelSave.system_message;
//            noReadModel.noReadXitongModelShow.message_url = noReadModel.noReadXitongModelXin.message_url;
//            
//            [XHQArchiverTool writeModel:noReadModel to:kFilePathNoReadModel];
////            发个通知?
//            [[NSNotificationCenter defaultCenter] postNotificationName: kNotifNoReadModelChang object:nil];
//
//            
//        }else{
//            
//            //                NSString *msg =   json[@"msg"];
//            //                [msg showAlert];
//        }
//        
//        
//    } failure:^(NSString *urlStr, NSError *error) {
//        //            [error.localizedDescription showAlert];
//    } timeOut:0 showMB:NO toView:nil];
//
//}
//
//
//+(void)checkFankuiModel
//{
//    
//    
//    if ([self readModel]) { //登录了 才去访问反馈的相关信息
//        NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
//        dicM[@"device_type"] = @"IPHONE";
//        dicM[@"hardware_sn"] = GGBS;
//        
//        //发送网络请求 ReTry
//        [XHQHttpTool xhqpost:[kNetAdressBase stringByAppendingString:@"get_feedback_count"] params:dicM success:^(NSString *urlStr, NSDictionary *json) {
//            if ([json[@"success"] intValue] == YES) {//[json[@"success"] intValue] == YES
//           
//                XMNoReadFankuiModel *model = [XMNoReadFankuiModel yy_modelWithJSON:json];
////                model.feedback_message = 10;
//                
//                //
//                NOReadModel *noReadModel = [XHQArchiverTool readModelWithPath:kFilePathNoReadModel];
//                if (noReadModel == nil) return ;
//                noReadModel.noReadFankuiModelXin = model;
//                noReadModel.noReadFankuiModelShow.feedback_message = noReadModel.noReadFankuiModelXin.feedback_message - noReadModel.noReadFankuiModelSave.feedback_message;
//                
//                noReadModel.noReadFankuiModelShow.feedback_url = noReadModel.noReadFankuiModelXin.feedback_url;
//                
//                [XHQArchiverTool writeModel:noReadModel to:kFilePathNoReadModel];
//                //            发个通知?
//                [[NSNotificationCenter defaultCenter] postNotificationName: kNotifNoReadModelChang object:nil];
//                
//
//                
//                
//                
//                //发通知 模型变化了
//                
//            }else{
//                
//                //                NSString *msg =   json[@"msg"];
//                //                [msg showAlert];
//            }
//            
//            
//        } failure:^(NSString *urlStr, NSError *error) {
////            [error.localizedDescription showAlert];
//        } timeOut:0 showMB:NO toView:nil];
//
//    }else{
//          [XHQArchiverTool writeModel:nil to:kFilePathNoReadFankuiModel];
//    }
//    
//}
//
//
//
//
//+(void)checkUserAccountChange
//{
//    if ([self readModel]) {
//        
//        XMUserModel *userModel = [self readModel];
//        NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
//        dicM[@"hardware_sn"] = GGBS;
//        dicM[@"device_type"] = @"IPHONE";
//        dicM[@"username"] = userModel.username;
//        dicM[@"password"] = userModel.password;
//        dicM[@"soft_version"] = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] stringByReplacingOccurrencesOfString:@"." withString:@""];
//        
//        
//        //    WS(weakSelf);
//        
//        //发送网络请求
//        [XHQHttpTool xhqpost:[kNetAdressBase stringByAppendingString:kNetAdressLogin] params:dicM success:^(NSString *urlStr, NSDictionary *json) {
//            if ([json[@"success"] intValue] == YES) {//[json[@"success"] intValue] == YES
//       
//                
//                
//            }else{
//                //登陆失败 ?  删掉本地用户模型
//                //             [XHQArchiverTool writeModel:nil to:kFilePathUserModel];
//                [self saveModel:nil];
//                [NSDC postNotificationName:kNotifLoginStaeChage object:nil];
//                
//                NSString *msg =   json[@"msg"];
//                [msg showAlert];
//            }
//            
//            
//        } failure:^(NSString *urlStr, NSError *error) {
//            //        [error.localizedDescription showAlert];
//        } timeOut:0 showMB:NO toView:nil];
//
//        
//    }
//}
//
//
//
//
//
//
//
//
///**
// * 1 后台运行的登陆 1用于上次用户等了了再次打开app时候调用
// 2 用于用户登陆成功 每个10分钟请求接口   返回用户是否过期 登陆成功再去获取用户的未读消息个数变化
// 3  用户在登陆页面  最好也用这个接口  方便统一处理 不过这个方法没有地方传账号密码
// */
//+(void)dengLu{
//
//    
//    NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
//    dicM[@"hardware_sn"] = GGBS;
//    dicM[@"device_type"] = @"IPHONE";
//
//    dicM[@"soft_version"] = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] stringByReplacingOccurrencesOfString:@"." withString:@""];
//
//    
////    WS(weakSelf);
//    
//    //发送网络请求
//    [XHQHttpTool xhqpost:[kNetAdressBase stringByAppendingString:kNetAdressLogin] params:dicM success:^(NSString *urlStr, NSDictionary *json) {
//        if ([json[@"success"] intValue] == YES) {//[json[@"success"] intValue] == YES
////             XMUserModel *model =    [XMUserModel yy_modelWithDictionary:json];
////                      //
////            
////            //获取用户信息成功
////            //发送通知  让监听者做变化
////            if (model.is_test) {//是测试用户
////                
////            }else{
////                model.guangGaoIDF  =GGBS;
////                [self saveModel:model];
//////#warning TXB说不要发  现在逻辑不明确就先注释吧
//////                  [NSDC postNotificationName:kNotifLoginStaeChage object:nil];
////            }
//            
//
//
//        }else{
//            //登陆失败 ?  删掉本地用户模型
////             [XHQArchiverTool writeModel:nil to:kFilePathUserModel];
//            [self saveModel:nil];
//            [NSDC postNotificationName:kNotifLoginStaeChage object:nil];
//            
//            NSString *msg =   json[@"msg"];
//            [msg showAlert];
//        }
//        
//        
//    } failure:^(NSString *urlStr, NSError *error) {
////        [error.localizedDescription showAlert];
//    } timeOut:0 showMB:NO toView:nil];
//    
//
//    
//}
//
//
//
////
////+(void)dengLu:(    void (^)()    )errorBlock{
////    
////    
////    NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
////    dicM[@"hardware_sn"] = GGBS;
////    dicM[@"device_type"] = @"IPHONE";
////    dicM[@"soft_version"] = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] stringByReplacingOccurrencesOfString:@"." withString:@""];
////    
////    
////    //    WS(weakSelf);
////    
////    //发送网络请求
////    [XHQHttpTool xhqpost:[kNetAdressBase stringByAppendingString:kNetAdressLogin] params:dicM success:^(NSString *urlStr, NSDictionary *json) {
////        if ([json[@"success"] intValue] == YES) {//[json[@"success"] intValue] == YES
////            XMUserModel *model =    [XMUserModel yy_modelWithDictionary:json];
////            model.guangGaoIDF  =GGBS;
////            [self saveModel:model];
////            
////            //获取用户信息成功
////            //发送通知  让监听者做变化
////            [NSDC postNotificationName:kNotifLoginStaeChage object:nil];
////            
////            
////        }else{
////            //登陆失败  删掉本地用户模型
////            //             [XHQArchiverTool writeModel:nil to:kFilePathUserModel];
////            [self saveModel:nil];
////            [NSDC postNotificationName:kNotifLoginStaeChage object:nil];
////            
//////            NSString *msg =   json[@"msg"];
////            [@"您的账号存在异常登录或者密码被修改" showAlert];
////        }
////        
////        
////    } failure:^(NSString *urlStr, NSError *error) {
////        //        [error.localizedDescription showAlert];
////    } timeOut:0 showMB:NO toView:nil];
////}
//
//
//
//
//
//+(void)saveModel:(id)model
//{
//    [XHQArchiverTool writeModel:model to:kFilePathUserModel];
//}
//
//
//
//
//+(id)readModel
//{
//    return  [XHQArchiverTool readModelWithPath:kFilePathUserModel];
//}
//
//
//
//
///**
// *  下一次打开APP 是否要请求用户信息
// *
// *  @param needLogin <#needLogin description#>
// */
////+(void)setNeedLogin:(BOOL)needLogin{
////    [NSUD setObject:@(needLogin) forKey:@"needLogin"];
////    [NSUD synchronize];
////
////}
//
//
////+(BOOL)needLogin{
////    
//////    id  obj =  [NSUD objectForKey:@"needLogin"];
//////    if (obj == nil) {
//////        return YES;
//////    }
//////    return [obj boolValue];
////   
////    if ( [self readModel] == nil) {
////        return NO;
////    }else{
////        return YES;
////    }
////}
//
//
////
//
//
//
//
//
//
//#pragma mark 通知方法
///**
// *  通知方法  处理未读消息个数
// */
////+(void)checkMessageCount{
////    
////    //如果状态是登陆 消息个数可能有值   没登陆消息个数没值
////    
////        
////        
////        //读取本地的 消息个数模型?
////        
////        //登陆了
////        NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
////        dicM[@"device_type"] = @"IPHONE";
////        dicM[@"hardware_sn"] = GGBS;
////        
////        //发送网络请求 ReTry
////        [XHQHttpTool xhqpost:[kNetAdressBase stringByAppendingString:kNetAdressMessgeCount] params:dicM success:^(NSString *urlStr, NSDictionary *json) {
////            if ([json[@"success"] intValue] == YES) {//[json[@"success"] intValue] == YES
////                /**
////                 *  {success : 1,
////                 feedback_url : http://www.xiongmao999.com/feedback/?username=&password=,
////                 feedback_message : 3,
////                 message_url : http://www.xiongmao999.com/message/?username=&password=,
////                 system_message : 2,
////                 now
////                 */
////                NOReadModel *model = [NOReadModel yy_modelWithJSON:json];
////                [XHQArchiverTool writeModel:model to:kFilePathNoReadModel];
////                //发通知 模型变化了
////
////            }else{
////                
//////                NSString *msg =   json[@"msg"];
//////                [msg showAlert];
////            }
////            
////            
////        } failure:^(NSString *urlStr, NSError *error) {
//////            [error.localizedDescription showAlert];
////        } timeOut:0 showMB:NO toView:nil];
////        
////        
////    
////    
////
////}
//
//
//
//
//+(void)requestUserModel
//{
//    NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
//    dicM[@"hardware_sn"] = GGBS;
//    dicM[@"device_type"] = @"IPHONE";
//    
//    dicM[@"soft_version"] = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] stringByReplacingOccurrencesOfString:@"." withString:@""];
//    
//    
//    //    WS(weakSelf);
//    
//    //发送网络请求
//    [XHQHttpTool xhqpost:[kNetAdressBase stringByAppendingString:kNetAdressLogin] params:dicM success:^(NSString *urlStr, NSDictionary *json) {
//        if ([json[@"success"] intValue] == YES) {//[json[@"success"] intValue] == YES
//                         XMUserModel *model =    [XMUserModel yy_modelWithDictionary:json];
//            
//                            [self saveModel:model];
//            
//            
//            
//            
//        }else{
//   
//        }
//        
//        
//    } failure:^(NSString *urlStr, NSError *error) {
//        //        [error.localizedDescription showAlert];
//    } timeOut:0 showMB:NO toView:nil];
//}
//
//
//
//
//
//
//+ (BOOL)xhqReturn_guanwang_buy{
//    nslogF
//    
//    
//    NSString *phone = [[NSUserDefaults standardUserDefaults] objectForKey:kuserAccountLast];
//    
//    NSLog(@"------%@",phone);
//    if(phone==nil||phone==NULL)
//    {
//        phone=@"";
//    }
//    //从沙盒里取值 没值就返回 yes    再去访问网络值 赋值给沙盒
//    //思路2  请求中  显示什么  请求失败 显示什么  请求成功显示什么
//    
//    NSString *version = [[NSUserDefaults standardUserDefaults] stringForKey:@"CFBundleShortVersionString"];
//    
//    NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
//    dicM[@"device_type"] = @"IPHONE";
//    dicM[@"hardware_sn"] = GGBS;
//    dicM[@"phone"] = phone;
//    dicM[@"soft_version"] = version;
//    
//    
//    
//
//    
//    [XHQHttpTool xhqReTryPost:[kNetAdressBase stringByAppendingString:@"sr/"] params:dicM success:^(NSString *urlStr, id json) {
//        
//        
//        NSString *strResult = [NSString stringWithFormat:@"%@",json[@"sr"]];
//        
//        [strResult saveToNSDefaultsWithKey:kYingCangGouMai];
//        
//    } failure:^(NSString *urlStr, NSError *error) {
//        
//    } timeOut:10 showMB:NO toView:nil];
//    
//    
//    
//    NSString *strShaHe =  [NSUD objectForKey:kYingCangGouMai];
//    
//    if (strShaHe == nil) {
//        return YES;
//    }else{
//        return [strShaHe boolValue];
//    }
//
//}
//
//
//
//+ (void)xhq2Return_guanwang_buyHidden:(void(^)()) hidden  show:(void(^)()) show{
//    nslogF
//    
//    
//    NSString *phone = [[NSUserDefaults standardUserDefaults] objectForKey:kuserAccountLast];
//    
//    NSLog(@"------%@",phone);
//    if(phone==nil||phone==NULL)
//    {
//        phone=@"";
//    }
//    //从沙盒里取值 没值就返回 yes    再去访问网络值 赋值给沙盒
//    //思路2  请求中  显示什么  请求失败 显示什么  请求成功显示什么
//    
//    NSString *version = [[NSUserDefaults standardUserDefaults] stringForKey:@"CFBundleShortVersionString"];
//    
//    NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
//    dicM[@"device_type"] = @"IPHONE";
//    dicM[@"hardware_sn"] = GGBS;
//    dicM[@"phone"] = phone;
//    dicM[@"soft_version"] = version;
//    
//    [XHQHttpTool xhqReTryPost:[kNetAdressBase stringByAppendingString:@"sr/"] params:dicM success:^(NSString *urlStr, id json) {
//        
//        
//        NSString *strResult = [NSString stringWithFormat:@"%@",json[@"sr"]];
//        if ([strResult boolValue]) {//影藏
//            
//            hidden();
//        }else{
//            show();
//        }
//        
////        [strResult saveToNSDefaultsWithKey:kYingCangGouMai];
//        
//    } failure:^(NSString *urlStr, NSError *error) {
//        
//    } timeOut:10 showMB:NO toView:nil];
//    
//    
////    NSString *strShaHe =  [NSUD objectForKey:kYingCangGouMai];
////    
////    if (strShaHe == nil) {
////        return YES;
////    }else{
////        return [strShaHe boolValue];
////    }
//    
//}
//

@end
